package com.atguigu.guli.service.cms.service.impl;

import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.mapper.AdMapper;
import com.atguigu.guli.service.cms.service.AdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 广告推荐 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-08
 */
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements AdService {

    @Cacheable(value = "ad",key = "'ads'")//指定当前方法缓存的数据在redis中的键
    // 模块：数据类型 ，value和key最终拼接为redis数据的key：ad::ads
    @Override
    public Map<String, Object> getAds() {
        List<Ad> ads = baseMapper.selectList(null);
        List<Ad> banners = new ArrayList<>();
        List<Ad> teachers = new ArrayList<>();
        List<Ad> courses = new ArrayList<>();
        for (Ad ad : ads) {
            if ("1".equals(ad.getTypeId())){
                banners.add(ad);
            }else if ("2".equals(ad.getTypeId())){
                teachers.add(ad);
            }else if ("3".equals(ad.getTypeId())){
                courses.add(ad);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("banners",banners);
        map.put("teachers",teachers);
        map.put("courses",courses);
        return map;
    }
}
